﻿using System;
using System.Web;
using Lin.Library.Extensions;

namespace Lin.Library.Web
{



    public class WebContext
    {
        #region ContentType
        /// <summary>
        /// HTML ContentType
        /// </summary>
        /// <returns></returns>
        public enum Content_Type
        {
            _3gp,
            a,
            ai,
            aif,
            aiff,
            asc,
            asf,
            asm,
            asx,
            atom,
            au,
            avi,
            bat,
            bin,
            bmp,
            bz2,
            c,
            cab,
            cc,
            chm,
            com,
            conf,
            cpp,
            crt,
            css,
            csv,
            cxx,
            deb,
            der,
            diff,
            djv,
            djvu,
            dll,
            dmg,
            doc,
            dot,
            dtd,
            dvi,
            ear,
            eml,
            eps,
            exe,
            f,
            f77,
            f90,
            flv,
            gem,
            gemspec,
            gif,
            gz,
            h,
            hh,
            htm,
            html,
            ico,
            ics,
            ifb,
            iso,
            jar,
            java,
            jnlp,
            jpeg,
            jpg,
            js,
            json,
            log,
            m3u,
            m4v,
            man,
            mathml,
            mbox,
            mdoc,
            me,
            mid,
            midi,
            mime,
            mml,
            mng,
            mov,
            mp3,
            mp4,
            mp4v,
            mpeg,
            mpg,
            ms,
            msi,
            odp,
            ods,
            odt,
            ogg,
            p,
            pas,
            pbm,
            pdf,
            pem,
            pgm,
            pgp,
            pkg,
            pl,
            pm,
            png,
            pnm,
            ppm,
            pps,
            ppt,
            ps,
            psd,
            py,
            qt,
            ra,
            rake,
            ram,
            rar,
            rb,
            rdf,
            roff,
            rpm,
            rss,
            rtf,
            ru,
            s,
            sgm,
            sgml,
            sh,
            sig,
            snd,
            so,
            svg,
            svgz,
            swf,
            t,
            tar,
            tbz,
            tcl,
            tex,
            texi,
            texinfo,
            text,
            tif,
            tiff,
            torrent,
            tr,
            txt,
            vcf,
            vcs,
            vrml,
            war,
            wav,
            wma,
            wmv,
            wmx,
            wrl,
            wsdl,
            xbm,
            xhtml,
            xls,
            xml,
            xpm,
            xsl,
            xslt,
            yaml,
            yml,
            zip
        }
        #endregion

        /// <summary>
        /// 判断当前页面是否接收到了Post请求
        /// </summary>
        /// <returns></returns>
        public static bool IsPost
        {
            get
            {
                return HttpContext.Current.Request.HttpMethod.Equals("POST");
            }
        }

        /// <summary>
        /// 判断当前页面是否接收到了Get请求
        /// </summary>
        /// <returns></returns>
        public static bool IsGet
        {
            get
            {
                return HttpContext.Current.Request.HttpMethod.Equals("GET");
            }
        }

        /// <summary>
        /// 返回当前页面是否是跨站提交
        /// </summary>
        /// <returns></returns>
        public static bool IsCrossSitePost
        {
            get
            {
                if (IsPost)
                {
                    if (UrlReferrer.Length < 7)
                    {
                        return true;
                    }
                    Uri u = new Uri(UrlReferrer);
                    return u.Host != Host;
                }
                return false;
            }
        }

        /// <summary>
        /// 获取页面请求类型(Get,Post,Head)
        /// </summary>
        public static string PostType
        {
            get
            {
                return HttpContext.Current.Request.HttpMethod;
            }

        }

        /// <summary>
        /// 得到主机头
        /// </summary>
        /// <returns></returns>
        public static string Host
        {
            get
            {
                return HttpContext.Current.Request.Url.Host;
            }
        }

        /// <summary>
        /// 获取有关客户端上次请求的 URL 的信息，该请求链接到当前的 URL。
        /// </summary>
        public static string UrlReferrer
        {
            get
            {
                return Convert.ToString(HttpContext.Current.Request.UrlReferrer);
            }
        }

        /// <summary>
        /// 返回 URL 字符串的编码结果
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns>解码结果</returns>
        public static string UrlDecode(string str)
        {
            return HttpUtility.UrlDecode(str);
        }

        /// <summary>
        /// 客户端IP地址
        /// </summary>
        public static string IPAddress
        {
            get
            {
                string result = String.Empty;
                result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (null == result || result == String.Empty)
                {
                    result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }
                if (null == result || result == String.Empty)
                {
                    result = HttpContext.Current.Request.UserHostAddress;
                }
                if (null == result || result == String.Empty || !StringExt.IsIP(result))
                {
                    return "0.0.0.0";
                }
                return result;
            }
        }

        #region 获取URL参数
        public static int GetInt(string param)
        {
            int val = 0;
            string value = string.Empty;

            value = HttpContext.Current.Request.QueryString[param];
            if (!string.IsNullOrEmpty(value))
            {
                if (int.TryParse(value, out val))
                {
                    return val;
                }
            }

            value = HttpContext.Current.Request.Form[param];
            if (!string.IsNullOrEmpty(value))
            {
                if (int.TryParse(value, out val))
                {
                    return val;
                }
            }

            return 0;
        }

        public static int GetInt(string param, int defValue)
        {
            int val = 0;
            string value = string.Empty;

            value = HttpContext.Current.Request.QueryString[param];
            if (!string.IsNullOrEmpty(value))
            {
                if (int.TryParse(value, out val))
                {
                    return val;
                }
            }

            value = HttpContext.Current.Request.Form[param];
            if (!string.IsNullOrEmpty(value))
            {
                if (int.TryParse(value, out val))
                {
                    return val;
                }
            }

            return defValue;
        }

        public static bool GetBool(string param)
        {
            bool val = false;
            string value = string.Empty;

            value = HttpContext.Current.Request.QueryString[param];
            if (!string.IsNullOrEmpty(value))
            {
                if (bool.TryParse(value, out val))
                {
                    return val;
                }
            }

            value = HttpContext.Current.Request.Form[param];
            if (!string.IsNullOrEmpty(value))
            {
                if (bool.TryParse(value, out val))
                {
                    return val;
                }
            }

            return false;
        }

        public static bool GetBool(string param, bool defValue)
        {
            bool val = false;
            string value = string.Empty;

            value = HttpContext.Current.Request.QueryString[param];
            if (!string.IsNullOrEmpty(value))
            {
                if (bool.TryParse(value, out val))
                {
                    return val;
                }
            }

            value = HttpContext.Current.Request.Form[param];
            if (!string.IsNullOrEmpty(value))
            {
                if (bool.TryParse(value, out val))
                {
                    return val;
                }
            }

            return defValue;
        }

        public static string GetString(string param)
        {
            string value = string.Empty;

            value = QueryString(param);

            if (string.IsNullOrEmpty(value))
            {
                return Form(param);
            }

            return value;


        }

        public static string GetString(string param, string defValue)
        {
            string value = string.Empty;

            value = HttpContext.Current.Request.Form[param];
            if (!string.IsNullOrEmpty(value))
            {
                return value.ToString();
            }

            value = HttpContext.Current.Request.QueryString[param];
            if (!string.IsNullOrEmpty(value))
            {
                return value.ToString();
            }

            return defValue;
        }

        public static string QueryString(string param)
        {
            param = HttpContext.Current.Request.QueryString[param];
            if (string.IsNullOrEmpty(param))
            {
                return string.Empty;
            }
            return param;
        }

        public static string Form(string param)
        {
            param = HttpContext.Current.Request.Form[param];
            if (string.IsNullOrEmpty(param))
            {
                return string.Empty;
            }
            return param;

        }
        #endregion

        #region 获取ContentType
        public static string ContentType(Content_Type type)
        {
            switch (type)
            {
                case Content_Type._3gp: return "video/3gpp";
                case Content_Type.a: return "application/octet-stream";
                case Content_Type.ai: return "application/postscript";
                case Content_Type.aif: return "audio/x-aiff";
                case Content_Type.aiff: return "audio/x-aiff";
                case Content_Type.asc: return "application/pgp-signature";
                case Content_Type.asf: return "video/x-ms-asf";
                case Content_Type.asm: return "text/x-asm";
                case Content_Type.asx: return "video/x-ms-asf";
                case Content_Type.atom: return "application/atom+xml";
                case Content_Type.au: return "audio/basic";
                case Content_Type.avi: return "video/x-msvideo";
                case Content_Type.bat: return "application/x-msdownload";
                case Content_Type.bin: return "application/octet-stream";
                case Content_Type.bmp: return "image/bmp";
                case Content_Type.bz2: return "application/x-bzip2";
                case Content_Type.c: return "text/x-c";
                case Content_Type.cab: return "application/vnd.ms-cab-compressed";
                case Content_Type.cc: return "text/x-c";
                case Content_Type.chm: return "application/vnd.ms-htmlhelp";
                case Content_Type.com: return "application/x-msdownload";
                case Content_Type.conf: return "text/plain";
                case Content_Type.cpp: return "text/x-c";
                case Content_Type.crt: return "application/x-x509-ca-cert";
                case Content_Type.css: return "text/css";
                case Content_Type.csv: return "text/csv";
                case Content_Type.cxx: return "text/x-c";
                case Content_Type.deb: return "application/x-debian-package";
                case Content_Type.der: return "application/x-x509-ca-cert";
                case Content_Type.diff: return "text/x-diff";
                case Content_Type.djv: return "image/vnd.djvu";
                case Content_Type.djvu: return "image/vnd.djvu";
                case Content_Type.dll: return "application/x-msdownload";
                case Content_Type.dmg: return "application/octet-stream";
                case Content_Type.doc: return "application/msword";
                case Content_Type.dot: return "application/msword";
                case Content_Type.dtd: return "application/xml-dtd";
                case Content_Type.dvi: return "application/x-dvi";
                case Content_Type.ear: return "application/java-archive";
                case Content_Type.eml: return "message/rfc822";
                case Content_Type.eps: return "application/postscript";
                case Content_Type.exe: return "application/x-msdownload";
                case Content_Type.f: return "text/x-fortran";
                case Content_Type.f77: return "text/x-fortran";
                case Content_Type.f90: return "text/x-fortran";
                case Content_Type.flv: return "video/x-flv";
                case Content_Type.gem: return "application/octet-stream";
                case Content_Type.gemspec: return "text/x-script.ruby";
                case Content_Type.gif: return "image/gif";
                case Content_Type.gz: return "application/x-gzip";
                case Content_Type.h: return "text/x-c";
                case Content_Type.hh: return "text/x-c";
                case Content_Type.htm: return "text/html";
                case Content_Type.html: return "text/html";
                case Content_Type.ico: return "image/vnd.microsoft.icon";
                case Content_Type.ics: return "text/calendar";
                case Content_Type.ifb: return "text/calendar";
                case Content_Type.iso: return "application/octet-stream";
                case Content_Type.jar: return "application/java-archive";
                case Content_Type.java: return "text/x-java-source";
                case Content_Type.jnlp: return "application/x-java-jnlp-file";
                case Content_Type.jpeg: return "image/jpeg";
                case Content_Type.jpg: return "image/jpeg";
                case Content_Type.js: return "application/javascript";
                case Content_Type.json: return "application/json";
                case Content_Type.log: return "text/plain";
                case Content_Type.m3u: return "audio/x-mpegurl";
                case Content_Type.m4v: return "video/mp4";
                case Content_Type.man: return "text/troff";
                case Content_Type.mathml: return "application/mathml+xml";
                case Content_Type.mbox: return "application/mbox";
                case Content_Type.mdoc: return "text/troff";
                case Content_Type.me: return "text/troff";
                case Content_Type.mid: return "audio/midi";
                case Content_Type.midi: return "audio/midi";
                case Content_Type.mime: return "message/rfc822";
                case Content_Type.mml: return "application/mathml+xml";
                case Content_Type.mng: return "video/x-mng";
                case Content_Type.mov: return "video/quicktime";
                case Content_Type.mp3: return "audio/mpeg";
                case Content_Type.mp4: return "video/mp4";
                case Content_Type.mp4v: return "video/mp4";
                case Content_Type.mpeg: return "video/mpeg";
                case Content_Type.mpg: return "video/mpeg";
                case Content_Type.ms: return "text/troff";
                case Content_Type.msi: return "application/x-msdownload";
                case Content_Type.odp: return "application/vnd.oasis.opendocument.presentation";
                case Content_Type.ods: return "application/vnd.oasis.opendocument.spreadsheet";
                case Content_Type.odt: return "application/vnd.oasis.opendocument.text";
                case Content_Type.ogg: return "application/ogg";
                case Content_Type.p: return "text/x-pascal";
                case Content_Type.pas: return "text/x-pascal";
                case Content_Type.pbm: return "image/x-portable-bitmap";
                case Content_Type.pdf: return "application/pdf";
                case Content_Type.pem: return "application/x-x509-ca-cert";
                case Content_Type.pgm: return "image/x-portable-graymap";
                case Content_Type.pgp: return "application/pgp-encrypted";
                case Content_Type.pkg: return "application/octet-stream";
                case Content_Type.pl: return "text/x-script.perl";
                case Content_Type.pm: return "text/x-script.perl-module";
                case Content_Type.png: return "image/png";
                case Content_Type.pnm: return "image/x-portable-anymap";
                case Content_Type.ppm: return "image/x-portable-pixmap";
                case Content_Type.pps: return "application/vnd.ms-powerpoint";
                case Content_Type.ppt: return "application/vnd.ms-powerpoint";
                case Content_Type.ps: return "application/postscript";
                case Content_Type.psd: return "image/vnd.adobe.photoshop";
                case Content_Type.py: return "text/x-script.python";
                case Content_Type.qt: return "video/quicktime";
                case Content_Type.ra: return "audio/x-pn-realaudio";
                case Content_Type.rake: return "text/x-script.ruby";
                case Content_Type.ram: return "audio/x-pn-realaudio";
                case Content_Type.rar: return "application/x-rar-compressed";
                case Content_Type.rb: return "text/x-script.ruby";
                case Content_Type.rdf: return "application/rdf+xml";
                case Content_Type.roff: return "text/troff";
                case Content_Type.rpm: return "application/x-redhat-package-manager";
                case Content_Type.rss: return "application/rss+xml";
                case Content_Type.rtf: return "application/rtf";
                case Content_Type.ru: return "text/x-script.ruby";
                case Content_Type.s: return "text/x-asm";
                case Content_Type.sgm: return "text/sgml";
                case Content_Type.sgml: return "text/sgml";
                case Content_Type.sh: return "application/x-sh";
                case Content_Type.sig: return "application/pgp-signature";
                case Content_Type.snd: return "audio/basic";
                case Content_Type.so: return "application/octet-stream";
                case Content_Type.svg: return "image/svg+xml";
                case Content_Type.svgz: return "image/svg+xml";
                case Content_Type.swf: return "application/x-shockwave-flash";
                case Content_Type.t: return "text/troff";
                case Content_Type.tar: return "application/x-tar";
                case Content_Type.tbz: return "application/x-bzip-compressed-tar";
                case Content_Type.tcl: return "application/x-tcl";
                case Content_Type.tex: return "application/x-tex";
                case Content_Type.texi: return "application/x-texinfo";
                case Content_Type.texinfo: return "application/x-texinfo";
                case Content_Type.text: return "text/plain";
                case Content_Type.tif: return "image/tiff";
                case Content_Type.tiff: return "image/tiff";
                case Content_Type.torrent: return "application/x-bittorrent";
                case Content_Type.tr: return "text/troff";
                case Content_Type.txt: return "text/plain";
                case Content_Type.vcf: return "text/x-vcard";
                case Content_Type.vcs: return "text/x-vcalendar";
                case Content_Type.vrml: return "model/vrml";
                case Content_Type.war: return "application/java-archive";
                case Content_Type.wav: return "audio/x-wav";
                case Content_Type.wma: return "audio/x-ms-wma";
                case Content_Type.wmv: return "video/x-ms-wmv";
                case Content_Type.wmx: return "video/x-ms-wmx";
                case Content_Type.wrl: return "model/vrml";
                case Content_Type.wsdl: return "application/wsdl+xml";
                case Content_Type.xbm: return "image/x-xbitmap";
                case Content_Type.xhtml: return "application/xhtml+xml";
                case Content_Type.xls: return "application/vnd.ms-excel";
                case Content_Type.xml: return "application/xml";
                case Content_Type.xpm: return "image/x-xpixmap";
                case Content_Type.xsl: return "application/xml";
                case Content_Type.xslt: return "application/xslt+xml";
                case Content_Type.yaml: return "text/yaml";
                case Content_Type.yml: return "text/yaml";
                case Content_Type.zip: return "application/zip";
                default: return "text/plain";
            }
        }
        #endregion
    }
}
